<?php

class  Goods_model extends CI_Model
{
    public function __construct()
    {
        $this->load->helper("url");
        $this->load->model('assets_model');
        $this->load->model('users_model');
    }

    /**
     * @param int $customer_id  客户id
     * @param string $unit_price  json字符串
     * @return mixed
     */
    function adjust_unit_price($customer_id=0,$unit_price=""){
        if ($customer_id && $unit_price){
            $post_arr['key'] = md5_key();
            $post_arr['customer_id']= $customer_id;
            $post_arr['unit_price']= $unit_price;
//            log_message("error",$post_arr['unit_price']);
            $url = $this->config->item("shop_base_url")."citymanagerapi/sync_single_product/";
            $rs = curl_request($url,$post_arr);
            $rs = json_decode($rs,true);
            if ($rs['code']==200){
                //此用户调价数
                $count = $rs['data']['adjust_count'];
                $map['id'] = $customer_id;
                $this->db->where($map)->update("customer",array("adjustments"=>$count));

                // 同步到企业站
                $shop_user_id = $this->db->where(array("id"=>$customer_id))->get("customer")->row_array()['shop_customer_id'];
                $url = $this->config->item("business_station_base_url")."customer_api/update_user_adjustments/";
                $data1['adjustments'] = $count;
                $data1['shop_user_id'] =$shop_user_id;
                //发送数据
                $res_data = curl_request($url,$data1);

                $data['status'] = "success";
                $data['msg'] = "调价成功";
                $data['data'] = "";
            }else{
                $data['status'] = "fail";
                $data['msg'] = $rs['msg'];
                $data['data'] = "";
            }
        }else{
            $data['msg'] = "参数错误";
            $data['status'] = "fail";
            $data['data'] = "";
        }
        return $data;
    }

    //总价浮动
    function adjust_all_price($customer_id = 0,$ratio=0){
        if($customer_id && $ratio){
            // 同步到企业站
            $shop_user_id = $this->db->where(array("id"=>$customer_id))->get("customer")->row_array()['shop_customer_id'];
            //同步到商城
            $url = $this->config->item("business_station_base_url")."customer_api/update_user_discount/";
            $data1['discount'] = $ratio;
            $data1['shop_user_id'] =$shop_user_id;
            //发送数据
            $res_data = curl_request($url,$data1);

            //同步到商城
            $url = $this->config->item("shop_base_url")."citymanagerapi/sync_all_product/";
            $data1['key'] = md5_key();
            $data1['ratio'] = $ratio;
            $data1['customer_id'] =$customer_id;
            //发送数据
            $get_data = curl_request($url,$data1);
            $get_data = json_decode($get_data,true);
            if ($get_data['code']==200){
                $map['id'] = $customer_id;
                $this->db->where($map)->update("customer",array("discount"=> $ratio));
                $data['msg'] = "调价成功";
                $data['status'] = "success";
                $data['data'] = "";
            }else{
                $data['msg'] = $get_data['msg'];
                $data['status'] = "fail";
                $data['data'] = "";
            }
        }else{
            $data['msg'] = "参数错误";
            $data['status'] = "fail";
            $data['data'] = "";
        }
        return $data;
    }

    //获取临时存储的单品调价信息
    function get_temp_unit_price($save_key=""){
        if($save_key){
            $rs = $this->db->where(array("key"=>$save_key))->get("key_value")->row_array();
            if($rs){
                return json_decode($rs['value'],true);
            }else{
                return false;
            }

        }else{
            return false;
        }

    }


}

